/*
 * DiscoveryFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.page;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.model.IDiscoveryModel;
import com.yidiandao.business.data.response.DiscoveryItemTypeResponse;
import com.yidiandao.business.ui.adapter.DiscoveryItemPageAdapter;
import com.yidiandao.business.ui.main.discovery.page.active.DiscoveryActivePageFragment;
import com.yidiandao.business.ui.main.discovery.page.active.DiscoveryActivePagePresenter;
import com.yidiandao.business.ui.main.discovery.page.cyclopedia.DiscoveryCyclopediaPageFragment;
import com.yidiandao.business.ui.main.discovery.page.cyclopedia.DiscoveryCyclopediaPagePresenter;
import com.yidiandao.business.ui.main.discovery.page.news.DiscoveryNewsPageFragment;
import com.yidiandao.business.ui.main.discovery.page.news.DiscoveryNewsPagePresenter;
import com.yidiandao.business.ui.main.discovery.page.wanttobuy.DiscoveryWantToBuyPageFragment;
import com.yidiandao.business.ui.main.discovery.page.wanttobuy.DiscoveryWantToBuyPagePresenter;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 发现-第一层 如 活动/新闻/百科
 */
public class DiscoveryPageFragment extends SuperBaseFragment implements DiscoveryPageContract.View {

    public static final String TAG = DiscoveryPageFragment.class.getSimpleName();

    private TabLayout mTabLayout;

    private ViewPager mViewpager;

    private DiscoveryPageContract.Presenter mPresenter;

    private DiscoveryItemPageAdapter mDiscoveryItemPageAdapter;

    /**
     * 标题的集合
     */
    private List<DiscoveryItemTypeResponse.DataBean.CatListBean> mTitleList = new ArrayList<>();

    private ArrayList<String> mTitles = new ArrayList<>();

    public static DiscoveryPageFragment getInstance(int typeId) {
        DiscoveryPageFragment fragment = new DiscoveryPageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Intents.DISCOVERY_TYPEID, typeId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initViews() {
        mTabLayout = (TabLayout) mRootView.findViewById(R.id.tab_layout);
        mViewpager = (ViewPager) mRootView.findViewById(R.id.viewpager);
    }

    @Override
    public int getTypeId() {
        return getArguments().getInt(Intents.DISCOVERY_TYPEID);
    }

    private void initDataAndEvent() {
        initData();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            if (null != savedInstanceState) {
                mDiscoveryItemPageAdapter = new DiscoveryItemPageAdapter(getChildFragmentManager());
                ArrayList<String> titles = savedInstanceState.getStringArrayList("title");
                mTitles.clear();
                mTitles.addAll(titles);
                List<Fragment> fragments = getChildFragmentManager().getFragments();
                if (null != fragments) {
                    for (int i = 0; i < fragments.size(); i++) {
                        Fragment fragment = fragments.get(i);
                        if (fragment instanceof DiscoveryActivePageFragment) {
                            DiscoveryActivePageFragment activePageFragment = (DiscoveryActivePageFragment) fragment;
                            new DiscoveryActivePagePresenter(activePageFragment);
                            mDiscoveryItemPageAdapter.addFragment(activePageFragment);
                        } else if (fragment instanceof DiscoveryNewsPageFragment) {
                            DiscoveryNewsPageFragment newsPageFragment = (DiscoveryNewsPageFragment) fragment;
                            new DiscoveryNewsPagePresenter(newsPageFragment);
                            mDiscoveryItemPageAdapter.addFragment(newsPageFragment);
                        } else if (fragment instanceof DiscoveryCyclopediaPageFragment) {
                            DiscoveryCyclopediaPageFragment cyclopediaPageFragment = (DiscoveryCyclopediaPageFragment) fragment;
                            new DiscoveryCyclopediaPagePresenter(cyclopediaPageFragment);
                            mDiscoveryItemPageAdapter.addFragment(cyclopediaPageFragment);
                        } else if (fragment instanceof DiscoveryWantToBuyPageFragment) {
                            DiscoveryWantToBuyPageFragment wantToBuyPageFragment = (DiscoveryWantToBuyPageFragment) fragment;
                            new DiscoveryWantToBuyPagePresenter(wantToBuyPageFragment);
                            mDiscoveryItemPageAdapter.addFragment(wantToBuyPageFragment);
                        }
                        mDiscoveryItemPageAdapter.addTitle(mTitles.get(i));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putStringArrayList("title", mTitles);
        super.onSaveInstanceState(outState);
    }

    private void initData() {
        if (null == mDiscoveryItemPageAdapter) {
            mDiscoveryItemPageAdapter = new DiscoveryItemPageAdapter(getChildFragmentManager());
            for (int i = 0; i < mTitleList.size(); i++) {
                String catName = mTitleList.get(i).getCatName();
                String catId = mTitleList.get(i).getCatId();

                switch (mTitleList.get(i).getType()) {
                    case IDiscoveryModel.Type.ACTIVE:
                        DiscoveryActivePageFragment activePageFragment = DiscoveryActivePageFragment.getInstance(catId);
                        new DiscoveryActivePagePresenter(activePageFragment);
                        mDiscoveryItemPageAdapter.addFragment(activePageFragment);
                        break;
                    case IDiscoveryModel.Type.NEWS:
                        DiscoveryNewsPageFragment newsPageFragment = DiscoveryNewsPageFragment.getInstance(catId);
                        new DiscoveryNewsPagePresenter(newsPageFragment);
                        mDiscoveryItemPageAdapter.addFragment(newsPageFragment);
                        break;
                    case IDiscoveryModel.Type.CYCLOPEDIA:
                        DiscoveryCyclopediaPageFragment cyclopediaPageFragment = DiscoveryCyclopediaPageFragment.getInstance(catId);
                        new DiscoveryCyclopediaPagePresenter(cyclopediaPageFragment);
                        mDiscoveryItemPageAdapter.addFragment(cyclopediaPageFragment);
                        break;
                    case IDiscoveryModel.Type.WANT_TO_BUY:
                        DiscoveryWantToBuyPageFragment wantToBuyPageFragment = DiscoveryWantToBuyPageFragment.getInstance();
                        new DiscoveryWantToBuyPagePresenter(wantToBuyPageFragment);
                        mDiscoveryItemPageAdapter.addFragment(wantToBuyPageFragment);
                        break;
                }
                mTitles.add(catName);
                mDiscoveryItemPageAdapter.addTitle(catName);
            }
        }
//        mViewpager.setOffscreenPageLimit(mTitleList.size());
        //viewpager加载adapter
        mViewpager.setAdapter(mDiscoveryItemPageAdapter);
        //TabLayout加载viewpager
        mTabLayout.setupWithViewPager(mViewpager);
        mTabLayout.setVisibility(mTitleList.size() > 1 ? View.VISIBLE : View.GONE);
//        ReflectUtils.setTabLayoutIndicatorPadding(mTabLayout, 15, 15);
        mViewpager.setOffscreenPageLimit(mTitleList.size());
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discovery_base_page;
    }

    @Override
    public void setPresenter(DiscoveryPageContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void setTitleList(List<DiscoveryItemTypeResponse.DataBean.CatListBean> list) {
        mTitleList.clear();
        mTitleList.addAll(list);
        initDataAndEvent();
    }
}
